package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.DBConn;
import com.zerowidth.db.utils.IOUtils;
import com.zerowidth.db.utils.TimeUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class SignDB {

    public static boolean checkExist(String sign) {
        String sql = String.format("SELECT * FROM sign_record WHERE sign_record = '%s'", sign);

        ResultSet rs = null;
        try {
            rs = DBConn.getInst().getStmt().executeQuery(sql);
            return rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            IOUtils.close(rs);
        }

        return false;
    }

    public static boolean insertSign(String sign, String url) {
        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);
        String sql = String.format("INSERT INTO sign_record (sign_record, handle_timestamp, request_url) VALUES ('%s', '%s', '%s')", sign, time, url);
        return DBHelper.executeUpdate(sql) > 0;
    }

}
